package com.app.twth.ui.cyclecount.location;


import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.app.Constant;
import com.app.twth.base.BaseActivity;
import com.app.twth.databinding.ActivityCyclecountLocationBinding;
import com.app.twth.ui.cyclecount.CycleCountViewModel;
import com.app.twth.utils.Utils;

import java.util.List;

/**
 * @describe :
 */
public class CCLocationActivity extends BaseActivity<ActivityCyclecountLocationBinding, CycleCountViewModel> {
    private String TAG = getClass().getSimpleName();
    CycleCountLocationQr cycleCountLocationQr;
    private CCLocationAdapter adapter;
    List<CycleCountLocationQr.SkuListDTO> inventoryList;

    @Override
    protected void initListener() {
        viewModel.cycleCountLocationQrLiveData.observe(this, data -> {
            if (Utils.isEmpty(data.getIbLpn())) {
                inventoryList = data.getSkuList();
                adapter.setData(inventoryList);
                return;
            }
            Bundle bundle = new Bundle();
            bundle.putSerializable("data", data);
            AppRouter.showActivityForResult(mContext, CCLocationSubmitActivity.class, bundle, Constant.ForResult2);
        });
        viewModel.submitLiveData.observe(this, data -> {
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);
            finish();
        });
        viewModel.errorResultCode.observe(this, code -> {
            showHint((String) code);
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    protected String setTitle() {
        return "CycleCount(Lpn)";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        setNext("Location Submit");
        Bundle bundle = getIntent().getBundleExtra(AppRouter.K_EXTRA);
        cycleCountLocationQr = (CycleCountLocationQr) bundle.getSerializable("data");
        inventoryList = cycleCountLocationQr.getSkuList();
        binding.tvLocation.setText(cycleCountLocationQr.getLocationCode());
        binding.tvCode.requestFocus();
        binding.tvCode.setOnEditorActionListener(EnterListenter);

        adapter = new CCLocationAdapter(this, inventoryList, position -> {
            barcodeStr = inventoryList.get(position).getIbLpn();
            if (Utils.isEmpty(barcodeStr)) {
                return;
            }
            viewModel.cycleCountLocationInventory(cycleCountLocationQr.getLocationCode(), barcodeStr);
        });
        LinearLayoutManager layoutManager2 = new LinearLayoutManager(this);
        layoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerView.setLayoutManager(layoutManager2);
        binding.recyclerView.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        bottomLayoutBinding.tvHint.setText("");
        initScan();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unScan();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_scan:
                setScan();
                break;
            case R.id.tv_reset:
                bottomLayoutBinding.tvHint.setText("");
                binding.tvCode.setText("");
                break;
            case R.id.tv_next:
                if (System.currentTimeMillis() - timeMillis < spaceTime) {//两秒内触发一次
                    return;
                }
                timeMillis = System.currentTimeMillis();
                CycleCountLocationQr submitBean = new CycleCountLocationQr();
                submitBean.setLocationCode(cycleCountLocationQr.getLocationCode());
                viewModel.cycleCountLocationSubmit(submitBean);
                break;
        }
    }


    private TextView.OnEditorActionListener EnterListenter = new TextView.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                barcodeStr = binding.tvCode.getText().toString();
                if (!Utils.isEmpty(barcodeStr)) {
                    viewModel.cycleCountLocationInventory(cycleCountLocationQr.getLocationCode(), barcodeStr);
                }

            }
            return false;
        }
    };

    @Override
    public void getScanninCode() {
        super.getScanninCode();

        bottomLayoutBinding.tvHint.setText("");
        binding.tvCode.setText(barcodeStr);
        viewModel.cycleCountLocationInventory(cycleCountLocationQr.getLocationCode(), barcodeStr);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.ForResult2 && resultCode == RESULT_OK) {
            bottomLayoutBinding.tvHint.setText("");
            binding.tvCode.setText("");
//            cycleCountLocationQr = (CycleCountLocationQr) data.getSerializableExtra("data");
//
//            List<CycleCountLocationQr.SkuListDTO> skuList = cycleCountLocationQr.getSkuList();
//            if (skuList == null || skuList.size() == 0) {
//                finish();
//                return;
//            }
//            inventoryList = skuList;
//            adapter.setData(inventoryList);
            viewModel.cycleCountLocationInventory(cycleCountLocationQr.getLocationCode(), null);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
